package com.luozongyao.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luozongyao.eduService.entity.EduChapter;
import com.luozongyao.eduService.entity.EduVideo;
import com.luozongyao.eduService.entity.chapter.ChapterVo;
import com.luozongyao.eduService.entity.chapter.VideoVo;
import com.luozongyao.eduService.mapper.EduChapterMapper;
import com.luozongyao.eduService.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luozongyao.eduService.service.EduVideoService;
import com.luozongyao.servicebase.config.exceptionHandler.lzyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-08-11
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //1.根据课程id查询所有章节
        QueryWrapper<EduChapter> chapterVoQueryWrapper = new QueryWrapper<>();
        chapterVoQueryWrapper.eq("course_id",courseId);
        List<EduChapter> chapterList = baseMapper.selectList(chapterVoQueryWrapper);

        //2.根据课程id查询所有小节
        QueryWrapper<EduVideo> VideoVoQueryWrapper = new QueryWrapper<>();
        VideoVoQueryWrapper.eq("course_id",courseId);
        final List<EduVideo> videoList = videoService.list(VideoVoQueryWrapper);

        //3.封装chapterList的所有章节
        List<ChapterVo> finalChapterVideoList = new ArrayList<>();
        for (int i = 0; i < chapterList.size(); i++) {
            EduChapter eduChapter = chapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            finalChapterVideoList.add(chapterVo);
            //4.封装videoList的所有小节
            List<VideoVo> videoVoList = new ArrayList<>();
            for (int j = 0; j < videoList.size(); j++) {
                EduVideo eduVideo = videoList.get(j);
                if (eduChapter.getId().equals(eduVideo.getChapterId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
        }
        return finalChapterVideoList;
    }

    //删除章节的方法
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapterId章节id 查询小节表（表里有chapterId字段） 如果有数据 ，不进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = videoService.count(wrapper);
        if (count>0){
            throw  new lzyException(20001,"有小节，不能删除");
        }else {
            int result = baseMapper.deleteById(chapterId);

            return result>0;
        }

    }
    //2根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
